package com.example.pt.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class HttpClientUtil {
    
    private final RestTemplate restTemplate;
    
    public HttpClientUtil() {
        this.restTemplate = new RestTemplate();
    }
    
    /**
     * 发送GET请求
     * 
     * @param url 请求URL
     * @return 响应内容
     */
    public String get(String url) {
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        return response.getBody();
    }
    
    /**
     * 发送POST请求
     * 
     * @param url 请求URL
     * @param requestBody 请求体
     * @return 响应内容
     */
    public String post(String url, Object requestBody) {
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestBody, String.class);
        return response.getBody();
    }
    
    /**
     * 发送带请求头的请求
     * 
     * @param url 请求URL
     * @param method 请求方法
     * @param headers 请求头
     * @param requestBody 请求体，可为null
     * @return 响应内容
     */
    public String exchange(String url, HttpMethod method, HttpHeaders headers, Object requestBody) {
        HttpEntity<?> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }
} 